package com.schoolgard.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.schoolgard.base.query.ConstraintEnum;

/**
 *
 * <li>功能简述:hibernate帮助类
 * <li>详细描述:
 * @author  luoyg
 * @version  [8.0 2010-8-24]
 * @see  [相关类/方法]
 * @since  8.0
 */
public class HibernateHelper {
    /**
     * 动态调用Restrictions方法来获得Crition
     * 根据传入的constraint枚举和Restrictions.eq .like等常用方法的两个量动态调用Restrictions方法
     * @param constraint Restrictions各种方法名的枚举
     * @param propertyName
     * @param value
     * @return
     * @throws IllegalArgumentException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static Criterion invokeRestritionMethod(ConstraintEnum constraint,String propertyName,Object value) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
        Method method = null;
        if(value instanceof Collection){
            method=Restrictions.class.getMethod(constraint.getMethodName(),String.class,Collection.class);
        }else{
            method=Restrictions.class.getMethod(constraint.getMethodName(),String.class,Object.class);
        }
        return (Criterion)method.invoke(null,propertyName,value);
    }
//    public List<Search> getSearchList(Class clazz){
//        Search search=new Search();
//        List<Search> searchs=new ArrayList<Search>();
//        Field[] fields =clazz.getDeclaredFields();
//        for(Field field:fields){
//            field.getName();
//        }
//    }
}
